<?php
/**
 * 总后台店铺管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/15
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\StoreModel;
use think\facade\Request;

class StoreController extends AdminBaseController
{
    /**
     * 自营商家
     */
    public function self_list(){
        $data=Request::param("");
        $where=$this->search($data);

        $ag=new StoreModel();
        $list=$ag->getALL($where);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 入驻商家
     */
    public function join_list(){
        $data=Request::param("");
        $where=$this->search($data);

        $ag=new StoreModel();
        $list=$ag->getALL($where,StoreModel::TYPE_R);

        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data){

        $where[]=["status",">",StoreModel::STATUS_DEL];

        if(!empty($data["title"])){
            $where[]=["title","=",$data["title"]];
        }

        if(!empty($data["real_person"])){
            $where[]=["real_person",'like',"%{$data['real_person']}%"];
        }

        return $where;
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function edit($id=0){
        $store_id=$id;

        if(Request::isPost()){
            $data=Request::param();

            $AM=new StoreModel();
            $status=$AM->adds($data);

            if($status['status']){
                $this->success($status['msg']);
            }else{
                $this->error($status['msg']);
            }
        }else{
            $info=StoreModel::getByStore_id($store_id);
            $this->assign("info",$info);
            $this->assign("id",$id);
            $this->assign("type",1);
            return $this->fetch();
        }
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=StoreModel::get($id);
        $am->status=$status;
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function del(){
        $id=Request::post("id");

        $AM=new AdminUserModel();
        $status=$AM->del($id);

        if($status['status']){
            $this->success($status['msg']);
        }else{
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(){
        $id=Request::post("ids");

        $AM=new AdminUserModel();

        $error=0;
        $sussuc=0;
        foreach ($id as $vo){
            $status=$AM->del($vo);
            if(!$status['status']){
                $error++;
                continue;
            }else{
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    public function update_pwd(){
        if(Request::isPost()){
            $uid=$this->uid;
            $old_pwd=Request::param("old_pwd");
            $new_pwd=Request::param("new_pwd");
            $real_pwd=Request::param("real_pwd");


            if(empty($old_pwd) || $old_pwd==""){
                $this->error("旧密码不能为空！");
            }

            if(empty($new_pwd) || $new_pwd==""){
                $this->error("新密码不能为空！");
            }

            if($real_pwd!=$new_pwd){
                $this->error("重复密码不正确！");
            }

            $au=AdminUserModel::get($uid);

            if(password($old_pwd,$au->salt)!=$au->password){
                $this->error("旧密码错误！");
            }

            $au->password=password($real_pwd,$au->salt);
            $status=$au->save();

            if($status){
                $this->success("修改成功！");
            }else{
                $this->error("修改失败！");
            }

        }else{
            return $this->fetch();
        }
    }
}